package com.my.study.redis;

//import com.jd.jim.cli.redis.jedis.Jedis;
import com.my.util.FileUtils;
import redis.clients.jedis.Jedis;

import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestJedis {

    public static void main(String[] args) {

        Jedis jedis = new Jedis("127.0.0.1", 6379);


        String key = "assignment_123";

        List<String> vals = new ArrayList<>();
        System.out.println("vals -----" + vals.size());
        try {
            jedis.del(key);
            String[] res = new String[1];
            res[0] = "1";
            Long a = jedis.rpush(key, res);
            System.out.println("res :" + a);
            System.out.println(jedis.type(key));
            jedis.setnx(key, "0");
            System.out.println(jedis.type(key));

            //jedis.del(key);
            String [] res2 = new String[0];
            a = jedis.rpush(key +  "111", res2);
            System.out.println("res :" + a);


        }catch(Exception e){
            e.printStackTrace();
        }




        jedis.close();


//        Object obj = jedis.eval("print('hello world')");
//
//        System.out.println("=============" + obj);
//
//        List<String> keys = new ArrayList<>();
//        List<String> vals = new ArrayList<>();
//
//
//        // 测试一 基本测试
//        Object eval = jedis.eval("return 1", keys, vals);
//        System.out.println("firs========" + eval);
//
//        // 测试二 eval  里面也可以是一个文件
//        keys.add("name");
//        Object eval2 = jedis.eval("local tab={}  for i=1,#KEYS do  tab[i] = redis.call('get',KEYS[i]) end return tab",
//                keys, vals);
//        System.out.println("2------------" + eval2);
//
//        // 测试三 scriptLoad
//        //好处：这样可以缓存到服务器，不用每次把lua脚本的内容传过去
//        String lua = "local tab={}  for i=1,#KEYS do  tab[i] = redis.call('get',KEYS[i]) end return tab";
//        String scriptLoad = jedis.scriptLoad(lua);
//
//        Object evalsha = jedis.evalsha(scriptLoad, keys, vals);
//        System.out.println("3----------" + evalsha);
//
//        System.out.println("result is :=======" + vals);
//
//        String getScript = FileUtils.loadLuaScript("getLua.lua");
//        System.out.println("getScript=====" + getScript);
//        Object obj4 = jedis.eval(getScript, Arrays.asList("name"), Arrays.asList("zlx"));
//        System.out.println("obj4 is: =======" + obj4);
//
//
//        String luaScript = FileUtils.loadLuaScript("unlock.lua");
//        System.out.println("luaScript is:   =====" + luaScript);
//
//        Object obj2 = jedis.eval(luaScript, Arrays.asList("name"), Arrays.asList("zlx"));
//        System.out.println("obj2=======" + obj2);


    }
}
